//
//  WHStatusCell.swift
//  WHSinaWeiBo
//
//  Created by 王寒 on 16/4/11.
//  Copyright © 2016年 wanghan. All rights reserved.
//

import UIKit
import SnapKit

class WHStatusCell: UITableViewCell {
    //工具条顶部的约束
    var tooBariTopCons: Constraint?
    var statusViewModel: WHStatusViewModel?{
        didSet{
            originalView.statusViewModel = statusViewModel
            statusToolBar.statusViewModel = statusViewModel
            //清空约束
            tooBariTopCons?.uninstall()
            //转发微博对cell复用设置
            if statusViewModel?.status?.retweeted_status != nil{
                //有转发微博
                //设置约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.tooBariTopCons = make.top.equalTo(statusRetweeted.snp_bottom).constraint
                })
                //显示转发微博
                statusRetweeted.hidden = false
                //设置数据
                statusRetweeted.statusViewModel = statusViewModel
            }else{
                //没有转发微博
                //设置约束
                statusToolBar.snp_updateConstraints(closure: { (make) -> Void in
                    self.tooBariTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
                //隐藏转发微博
                statusRetweeted.hidden = true
                //清空数据
                statusRetweeted.statusViewModel = nil
            }
        }
    }
    
    //懒加载控件
    private lazy var originalView: WHStatusOriginalView = WHStatusOriginalView()
    private lazy var statusToolBar: WHStatusToolBar = WHStatusToolBar()
    private lazy var statusRetweeted: WHStatusRetweetedView = WHStatusRetweetedView()
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
        selectionStyle = .None
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK:添加子视图
    func setupUI(){
        backgroundColor = randomColor()
        
        //添加原创视图
        self.contentView.addSubview(originalView)
        self.contentView.addSubview(statusRetweeted)
        self.contentView.addSubview(statusToolBar)
        
        //约束
        //原创
        originalView.snp_makeConstraints(closure: { (make) -> Void in
            make.left.top.right.equalTo(contentView)
        })
        //转发
        statusRetweeted.snp_makeConstraints { (make) -> Void in
            make.left.right.equalTo(contentView)
            make.top.equalTo(originalView.snp_bottom)
            //不设置高度 留在给与数据时设置
        }
        
        
        //底部
        statusToolBar.snp_makeConstraints(closure: { (make) -> Void in
            self.tooBariTopCons = make.top.equalTo(statusRetweeted.snp_bottom).constraint
            make.left.right.equalTo(contentView)
            make.height.equalTo(36)
            
        })
        
        //contentView的约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.left.right.top.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
        }
            
            
            //先设置测试高度
//            make.bottom.equalTo(<#T##other: CGFloat##CGFloat#>)
        
    }
    
    
}


